package com.wish.g04.house.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g04.base.entity.*;
import com.wish.g04.base.service.IBusinessCircleService;
import com.wish.g04.base.service.IPremisesInfoService;
import com.wish.g04.base.service.ISubwayService;
import com.wish.g04.common.core.utils.poi.ExcelUtil;
import com.wish.g04.common.core.web.controller.BaseController;
import com.wish.g04.common.core.web.domain.AjaxResult;
import com.wish.g04.common.core.web.page.TableDataInfo;
import com.wish.g04.house.dto.PremisesInfoDto;
import com.wish.g04.house.service.TbPremisesInfoService;
import com.wish.g04.system.api.domain.DictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 楼盘信息Controller
 * 
 * @author xumengyang
 * @date 2022-05-23
 */
@RestController
@RequestMapping("/house")
public class TbPremisesInfoController extends BaseController
{

//service 层
    @Autowired
    private TbPremisesInfoService tbPremisesInfoService;
//base  service 层
    @Autowired
    private IPremisesInfoService IPremisesInfoService;
//商户service
    @Autowired
    private IBusinessCircleService ibusinessCircleService;
//地铁
    @Autowired
    private ISubwayService iSubwayService;

    /**
     * 查询楼盘信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PremisesInfoDto tbPremisesInfoDto)
    {
        startPage();
        return tbPremisesInfoService.getList(tbPremisesInfoDto);
    }

    /**
     * 导出楼盘信息列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, PremisesInfo tbPremisesInfo) throws IOException
    {
        List<PremisesInfo> list = IPremisesInfoService.list();
        ExcelUtil<PremisesInfo> util = new ExcelUtil<PremisesInfo>(PremisesInfo.class);
        util.exportExcel(response, list, "info");
    }

    /**
     * 获取楼盘信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return  tbPremisesInfoService.getInfo(id);
    }

    /**
     * 新增楼盘信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody PremisesInfoDto premisesInfoDto)
    {
        tbPremisesInfoService.save(premisesInfoDto);
        return AjaxResult.success();
    }

    /**
     * 修改楼盘信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody PremisesInfo tbPremisesInfo)
    {
        IPremisesInfoService.save(tbPremisesInfo);
        return AjaxResult.success();
    }

    /**
     * 删除楼盘信息
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        List<Long> longs = Arrays.asList(ids);
        IPremisesInfoService.removeByIds(longs);
        return AjaxResult.success();
    }

    /**
     * 地区信息
     */
    @GetMapping("/region/{parentId}")
    public AjaxResult region(@PathVariable("parentId") Integer parentId)
    {
        List<Region> list = tbPremisesInfoService.getRegion(parentId);
        return AjaxResult.success(list);
    }

    /**
     * 根据字典类型查询字典数据信息 通过feign调用
     */
    @GetMapping(value = "/getDictDataByType/{dictType}")
    public AjaxResult getDictDataByType(@PathVariable String dictType)
    {
        List<DictData> dictDataList = tbPremisesInfoService.getDictDataByType(dictType);

        return AjaxResult.success(dictDataList);
    }

    /**
     * 根据楼盘特色
     */
    @GetMapping(value = "/getPremisesProperty")
    public AjaxResult getPremisesProperty()
    {
        List<ConfigPremisesProperty> configPremisesProperties = tbPremisesInfoService.getPremisesProperty();

        return AjaxResult.success(configPremisesProperties);
    }

    /**
     * 商圈  根据tb_business_circle  code字段查询 去对应商圈
     */
    @GetMapping(value = "/businessCircle/{code}")
    public AjaxResult businessCircle(@PathVariable("code")String code)
    {
        QueryWrapper<BusinessCircle> wrapper = new QueryWrapper<>();
        wrapper.eq("code",code);
        List<BusinessCircle> list = ibusinessCircleService.list(wrapper);
        return AjaxResult.success(list);
    }

    /**
     * 商圈  根据tb_business_circle  code字段查询 去对应商圈
     */
    @GetMapping(value = "/subwayListById/{city}")
    public AjaxResult subwayListById(@PathVariable("city")Integer city)
    {
        QueryWrapper<Subway> wrapper = new QueryWrapper<>();
        wrapper.eq("city",city);
        List<Subway> list = iSubwayService.list(wrapper);
        return AjaxResult.success(list);
    }
}
